package com.zhump.springcloud.fegin.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zhump.springcloud.fegin.fegin.MemberServiceFegin;
import com.zhump.springcloud.fegin.member.service.MemberService;
import com.zhump.springcloud.fegin.member.service.User;
import com.zhump.springcloud.fegin.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 会员服务实现
 */
@RestController
public class OrderServiceImpl implements OrderService {
    @Autowired
    private MemberServiceFegin memberServiceFegin;
    @RequestMapping("/orderToMember")
    @Override
    public String orderToMember(@RequestParam("name") String name) {
        User user = memberServiceFegin.getMember(name);
        return user == null ? "没有找到用户信息": user.toString();
    }

    //没有解决服务雪崩效应
    @RequestMapping("/orderToMemberGetUserInfo")
    @Override
    public ModelMap orderToMemberGetUserInfo() {
        ModelMap userInfo = memberServiceFegin.getUserInfo("未解决雪崩效应-订单服务调");
        return userInfo;
    }


    //解决服务雪崩效应
    @HystrixCommand(fallbackMethod = "orderToMemberGetUserInfoHystrixFallbackMethod")
    @RequestMapping("/orderToMemberGetUserInfoHystrix")
    public ModelMap orderToMemberGetUserInfoHystrix() {
        System.out.println("orderToMemberGetUserInfoHystrix-线程池名称:"+Thread.currentThread().getName());
        ModelMap userInfo = memberServiceFegin.getUserInfo("解决雪崩效应-订单服务调");
        return userInfo;
    }

    /**
     * 服务降级方法
     * @return
     */
    public ModelMap orderToMemberGetUserInfoHystrixFallbackMethod(){
        ModelMap modelMap = new ModelMap();
        modelMap.put("code","1");
        modelMap.put("msg","返回一个友好的提示语，服务降级测试");
        modelMap.put("data",null);
        return modelMap;
    }

    @RequestMapping("/orderInfo")
    @Override
    public String orderInfo() {
        System.out.println("orderInfo-线程池名称:"+Thread.currentThread().getName());
        return "orderInfo方法执行。。。。。。";
    }
}
